package org.example.order.controller;

import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.example.order.common.APIResponse;
import org.example.order.common.ResponseCode;
import org.example.order.entity.Order;
import org.example.order.service.OrderService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @PostMapping("/add")
    public APIResponse add(@RequestBody Order order) {
        orderService.add(order);
        return new APIResponse(ResponseCode.SUCCESS, "添加成功");
    }

    @DeleteMapping("/deleteById/{id}")
    public APIResponse deleteById(@PathVariable Long id) {
        orderService.deleteById(id);
        return new APIResponse(ResponseCode.SUCCESS, "删除成功");
    }

    @DeleteMapping("/deleteBatch")
    public APIResponse deleteBatch(@RequestBody List<Long> ids) {
        orderService.deleteBatch(ids);
        return new APIResponse(ResponseCode.SUCCESS, "批量删除成功");
    }

    @PutMapping("/update")
    public APIResponse update(@RequestBody Order order) {
        orderService.update(order);
        return new APIResponse(ResponseCode.SUCCESS, "更新成功");
    }

    @GetMapping("/selectById/{id}")
    public APIResponse selectById(@PathVariable Long id) {
        Order order = orderService.selectById(id);
        return new APIResponse(ResponseCode.SUCCESS, "查询成功", order);
    }

    @GetMapping("/selectAll")
    public APIResponse selectAll() {
        List<Order> list = orderService.selectAll();
        return new APIResponse(ResponseCode.SUCCESS, "查询成功", list);
    }

    @GetMapping("/selectPage")
    public APIResponse selectPage(
            Order order,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Order> pageInfo = orderService.selectPage(order, pageNum, pageSize);
        return new APIResponse(ResponseCode.SUCCESS, "分页查询成功", pageInfo);
    }
}
